import Vue from 'vue'
import {
  base64
} from 'vux'

Vue.filter('shortHtml', (html, count = 0) => {
  if (!html) return ''
  let tmp = document.createElement('div')
  const afterEscape = html.replace(/<\s?img[^>]*>/gi, '')
  tmp.innerHTML = afterEscape
  let text = tmp.textContent || tmp.innerText || ''
  if (!count || text.length > count) {
    return text.substring(0, count)
  } else {
    return text || ''
  }
})

// 日期格式化
Vue.filter('dataFormat', (date, fmt) => {
  var o = {
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    'S': date.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  }
  return fmt
})

Vue.filter('customTime', (item) => {
  if (!item) return ''
  // now
  let nowTime = new Date().getTime()
  let minuteTime = 60 * 1000
  let hourTime = 60 * minuteTime
  let dayTime = 24 * hourTime
  let monthTime = dayTime * 30
  let yearTime = monthTime * 12
  // history
  let publish = new Date(item)
  let publishTime = publish.getTime()
  let date = publish.getDate()
  let year = publish.getFullYear()
  let month =
    publish.getMonth() < 9 ?
    `0${publish.getMonth() + 1}` :
    publish.getMonth() + 1
  let historyTime = parseInt(nowTime) - parseInt(publishTime)
  let descTime
  if (historyTime >= yearTime) {
    // 按年算
    descTime = `${year}-${month}-${date}`
  } else if (historyTime < yearTime && historyTime >= monthTime) {
    // 按月算
    descTime = `${month}-${date}`
  } else if (historyTime < monthTime && historyTime >= dayTime) {
    // 按天算
    descTime = parseInt(historyTime / dayTime) + '天前'
  } else if (historyTime < dayTime && historyTime >= hourTime) {
    // 按小时算
    descTime = parseInt(historyTime / hourTime) + '小时前'
  } else if (historyTime < hourTime && historyTime >= minuteTime) {
    // 按分钟算
    descTime = parseInt(historyTime / minuteTime) + '分钟前'
  } else {
    descTime = '刚刚'
  }
  return descTime
})

/**
 * @description 超过显示的最大长度部分则以...继续显示
 * @param {String} val 文本
 * @param {Number} showLength 需要显示最大的长度
 */
Vue.filter('lengthLimit', (val, showLength = 20) => {
  if (val === 'undefined') {
    return ''
  } else {
    if (val != null) {
      if (val.length > showLength) {
        return val.substring(0, showLength) + '...'
      } else {
        return val
      }
    }
  }
})

/**
 * @description 用户类型格式
 */
Vue.filter('userTypeFormat', (val) => {
  function userTypeFormat(num) {
    let str
    switch (num) {
      case 1:
        str = '球迷'
        break
      case 2:
        str = '运动员'
        break
      case 3:
        str = '裁判员'
        break
    }
    return str
  }
  return userTypeFormat(val)
})

/**
 * @description 超过显示的最大长度部分则以...全文继续显示
 * @param {String} val 文本
 * @param {Number} showLength 需要显示最大的长度
 */
Vue.filter('lengthLimitWithFulltext', (val, showLength = 20) => {
  if (val === 'undefined') {
    return ''
  } else {
    if (val.length > showLength) {
      let str = '<span style="color: #476099">...全文</span>'
      return val.substring(0, showLength) + str
    } else {
      return val
    }
  }
})

/**
 * @description 格式化图片后缀
 */
Vue.filter('formatImgSuffix', (val, prefix) => {
  if (val === 'undefined') {
    return ''
  } else {
    let appName = 'BYBC'
    let ossProcess = '?x-oss-process=image/resize,w_300,h_300/watermark,type_d3F5LXplbmhlaQ,size_13,color_FFFFFF,shadow_50,t_100,g_se,x_10,y_10,text_'
    if (val.length > 20) { // 必须是URL安全base64编码 encodeText = url_safe_base64_encode(fontText) 最大长度为64个字符(即支持汉字最多20个左右)
      let sign = appName + '@' + val.substring(0, 14)
      let base64Sign = base64.encode(sign)
      return prefix + ossProcess + base64Sign
    } else {
      let sign = appName + '@' + val
      let base64Sign = base64.encode(sign)
      return prefix + ossProcess + base64Sign
    }
  }
})

/**
 * @description 阅读量格式化
 */
Vue.filter('formatReading', (val, num = 100000) => {
  if (val === 'undefined') {
    return ''
  } else {
    if (val >= num) {
      return '10万+'
    } else {
      return val
    }
  }
})
